package com.zyiot.Dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zyiot.model.Messages;

public class MessageDAO {
	private DBOpenHelper helper;
	private SQLiteDatabase db;

	public MessageDAO(Context context) {
		helper = new DBOpenHelper(context);
	}

	/**
	 * 添加新的推送信息
	 * 
	 * @param student
	 */
	public void add(Messages message) {
		db = helper.getWritableDatabase();
		db.execSQL("insert into e_Message (msg_title,msg_content,msg_url,msg_date) values (?,?,?,?)",
				new Object[] { message.getMsg_title(), message.getMsg_content(), message.getMsg_url(), message.getMsg_date() });
	}

	/**
	 * 更新推送信息
	 * 
	 * @param student
	 */
	public void update(Messages message) {
		db = helper.getWritableDatabase();
		db.execSQL("update e_Message set msg_title = ?,msg_content = ? where msg_id = ?",
				new Object[] { message.getMsg_title(), message.getMsg_content(), message.getMsg_id() });
	}

	/**
	 * 刪除推送信息
	 * 
	 * @param sids
	 */
	public void detele(Integer[] msg_ids) {
		if (msg_ids.length > 0) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < msg_ids.length; i++) {
				sb.append('?').append(',');
			}
			sb.deleteCharAt(sb.length() - 1);
			SQLiteDatabase database = helper.getWritableDatabase();
			database.execSQL("delete from e_message where msg_id in (" + sb + ")", msg_ids);
		}
	}

	/**
	 * 获取推送信息
	 * 
	 * @param start
	 *            其实位置
	 * @param count
	 *            推送信息数量
	 * @return
	 */
	public List<Messages> getScrollData(int start, int count) {

		List<Messages> messages = new ArrayList<Messages>();
		db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from e_message limit ?,?", new String[] { String.valueOf(start), String.valueOf(count) });
		while (cursor.moveToNext()) {
			messages.add(new Messages(cursor.getInt(cursor.getColumnIndex("msg_id")), cursor.getString(cursor.getColumnIndex("msg_title")), cursor
					.getString(cursor.getColumnIndex("msg_content")), cursor.getString(cursor.getColumnIndex("msg_url")), cursor.getString(cursor
					.getColumnIndex("msg_date")), cursor.getString(cursor.getColumnIndex("msg_flag"))));
		}
		return messages;
	}

	public List<Messages> getScrollDataNews(int start, int count) {
		List<Messages> ls_messages = new ArrayList<Messages>();

		db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from e_message limit ?,?", new String[] { String.valueOf(start), String.valueOf(count) });
		while (cursor.moveToNext()) {
			Messages message = new Messages();
			message.setMsg_id(cursor.getInt(cursor.getColumnIndex("msg_id")));
			message.setMsg_title(cursor.getString(cursor.getColumnIndex("msg_title")));
			message.setMsg_content(cursor.getString(cursor.getColumnIndex("msg_content")));
			message.setMsg_url(cursor.getString(cursor.getColumnIndex("msg_url")));
			message.setMsg_date(cursor.getString(cursor.getColumnIndex("msg_date")));
			message.setMsg_flag(cursor.getString(cursor.getColumnIndex("msg_flag")));

			ls_messages.add(message);
		}
		return ls_messages;
	}

	public List<HashMap<String, Object>> getScrollDataNewsMap(int start, int count) {
		List<HashMap<String, Object>> messages = new ArrayList<HashMap<String, Object>>();
		db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from e_message ORDER BY msg_id DESC limit ?,?", new String[] { String.valueOf(start), String.valueOf(count) });
		while (cursor.moveToNext()) {
			HashMap<String, Object> hashMap = new HashMap<String, Object>();
			hashMap.put("msg_id", cursor.getInt(cursor.getColumnIndex("msg_id")));
			hashMap.put("msg_title", cursor.getString(cursor.getColumnIndex("msg_title")));
			hashMap.put("msg_content", cursor.getString(cursor.getColumnIndex("msg_content")));
			hashMap.put("msg_url", cursor.getString(cursor.getColumnIndex("msg_url")));
			hashMap.put("msg_date", cursor.getString(cursor.getColumnIndex("msg_date")));
			hashMap.put("msg_flag", cursor.getString(cursor.getColumnIndex("msg_flag")));
			messages.add(hashMap);
		}
		return messages;
	}

	/**
	 * 获取推送信息数量
	 * 
	 * @return
	 */
	public long getCount() {
		db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select count(msg_id) from e_message", null);
		if (cursor.moveToNext()) {
			return cursor.getLong(0);
		}
		return 0;
	}
}
